package com.jzx.provider.cv.service;

import com.jzx.provider.common.utils.BeanUtil;
import com.jzx.provider.common.utils.DateUtil;
import com.jzx.provider.cv.dto.command.ProjectInfoCommand;
import com.jzx.provider.cv.model.ProjectInfo;
import com.jzx.provider.cv.repository.CvRepository;
import com.jzx.provider.cv.repository.ProjectInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author daihuihui
 * @date 2019/2/5
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectInfoService {
    @Autowired
    private ProjectInfoRepository projectInfoRepository;

    @Autowired
    private CvRepository cvRepository;

    public void saveOrUpdate(ProjectInfoCommand projectInfoCommand) {
        ProjectInfo projectInfo = ProjectInfo.builder().build();
        BeanCopier.create(ProjectInfoCommand.class, ProjectInfo.class, false)
                .copy(projectInfoCommand, projectInfo, null);
        projectInfoRepository.save(projectInfo);
        //更新简历时间
        cvRepository.findById(projectInfoCommand.getCvId()).ifPresent(cv -> {
            cvRepository.save(cv);
            //更新es索引
        });
    }

    public void deleteProject(Integer id) {
        projectInfoRepository.findById(id).ifPresent(projectInfo -> {
            projectInfoRepository.deleteById(id);
            //更新简历时间
            cvRepository.findById(projectInfo.getCvId()).ifPresent(cv -> {
                cv.setUpdateTime(new Date());
                cvRepository.save(cv);
                //更新es索引
            });
        });
    }

    public void saveOrUpdateProjectInfo(ProjectInfoCommand projectInfoCommand) {
        //处理开始和结束日期
        if (!"至今".equals(projectInfoCommand.getStartDate())) {
            projectInfoCommand.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(projectInfoCommand.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        if (!"至今".equals(projectInfoCommand.getEndDate())) {
            projectInfoCommand.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(projectInfoCommand.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        if (null != projectInfoCommand.getId()) {
            ProjectInfo projectInfo = ProjectInfo.builder().build();
            BeanCopier.create(ProjectInfoCommand.class, ProjectInfo.class, false).copy(projectInfoCommand, projectInfo, null);
            projectInfo.setUpdateTime(new Date());
            projectInfoRepository.updateCvProjectInfo(projectInfo);
        } else {
            ProjectInfo projectInfo = ProjectInfo.builder().build();
            BeanCopier.create(ProjectInfoCommand.class, ProjectInfo.class, false).copy(projectInfoCommand, projectInfo, null);
            projectInfo.setCreateTime(new Date());
            projectInfo.setUpdateTime(new Date());
            projectInfoRepository.save(projectInfo);
        }

    }
}
